package com.fzu.ess.sys.service.Impl;

import com.fzu.ess.sys.dao.MenuDao;
import com.fzu.ess.sys.entity.Menu;
import com.fzu.ess.sys.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ercha on 2015/10/21.
 */
@Service
public class MenuServiceImpl implements MenuService{
    @Autowired
    private MenuDao menuDao;

    @Override
    public String create(Menu menu){
        try {
            return (String) menuDao.save(menu);
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean delete(Menu menu){
        try {
            String hqlToDel = "delete from Menu r where r.id=:id";
            Map<String, Object> params = new HashMap<>();
            params.put("id", menu.getId());
            menuDao.executeHql(hqlToDel, params);

            /** 删除后代节点 */
            final String deleteDescendantsSql = "delete from Menu where parentIds like :parentIds";
            Map<String, Object> params2 = new HashMap<>();
            params2.put("parentIds", menu.getParentIds() + "%");
            menuDao.executeHql(deleteDescendantsSql, params2);

            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Menu menu){
        try{
            menuDao.merge(menu);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Menu> getAll() {
        try{
            return menuDao.find("from Menu");
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
//
//    @Override
//    public int create(int parentId, Menu menu) {
//        Menu parentMenu = menuDao.get("from Menu m where m.id = " + parentId);
//        int leftNum = parentMenu.getRightNum();
//        adjustSpace(1, leftNum, 1);
//
//        menu.setParentId(parentId);
//        menu.setLeftNum(leftNum);
//        menu.setRightNum(leftNum + 1);
//
//        return (Integer)menuDao.save(menu);
//    }
//
//    @Override
//    public int delete(Menu menu) {
//        int left = menu.getLeftNum();
//        int right = menu.getRightNum();
//        int cnt = menuDao.executeHql("delete from Menu m where m.leftNum between " + menu.getLeftNum() + " and " + menu.getRightNum());
//        adjustSpace((right-left+1)>>1, right+1, -1);
//        return cnt;
//    }
//
//    @Override
//    public int deleteById(int id) {
//        return delete(findById(id));
//    }
//
//    @Override
//    public void update(Menu menu) {
//        menuDao.update(menu);
//    }
//
//    @Override
//    public Menu findById(int id) {
//        return menuDao.get("from Menu m where m.id = " + id);
//    }
//
//    @Override
//    public List<Menu> getAllMenu() {
//        return menuDao.find("from Menu");
//    }
//
//    @Override
//    public int move(int sourceId, int targetId) {
//        Menu s = findById(sourceId);
//        Menu t = findById(targetId);
//
//        int preLeft = s.getLeftNum(), preRight = s.getRightNum();
//        int count = (preRight - preLeft + 1)>>1;
//        int parentRight = t.getRightNum();
//
//        //剪切
////        menuDao.executeHql("update Menu m set m.leftNum = m.leftNum - " + INF +" , m.rightNum = m.rightNum - " + INF +
////                " where m.leftNum between " + preLeft + " and " + preRight);
//        menuDao.executeHql("update Menu m set m.leftNum = -m.leftNum, m.rightNum = -m.rightNum" +
//                " where m.leftNum between " + preLeft + " and " + preRight);
//        adjustSpace(count, preRight + 1, -1);
//        System.out.println("should be 5, now is: " + t.getRightNum());
//
//        //粘贴
//        adjustSpace(count, parentRight, 1);
//        s.setParentId(targetId);
//        int distance = parentRight - preLeft;
//        return menuDao.executeHql("update Menu m set m.leftNum = -m.leftNum + " + distance +
//        ", m.rightNum = -m.rightNum + " + distance + " where m.leftNum < 0");
//    }
//
//    //count：节点的个数
//    //rightNum：比本节点的右值大 1
//    //type：1 为增加空间，-1 为减少空间
//    private int adjustSpace(int count, int rightNum, int type){
//        return menuDao.executeHql("update Menu m set m.rightNum = m.rightNum + " + 2*count*type +
//                " where m.rightNum >= " + rightNum);
//    }

}
